package design.pattern.Lu15CompositePattern;

import java.util.ArrayList;
import java.util.List;

/**
 * @program JavaBase
 * @description: 透明的组合模式
 * @author: zhanglu
 * @create: 2020-04-10 08:45:00
 */
public class CompositePattern03 {

    public static void main(String[] args) {

        Component03 root = new Composite03("A");
        Component03 branch = new Composite03("B");

        Leaf03 leaf = new Leaf03("C");

        branch.add(leaf);
        root.add(branch);

        show(root);

    }

    public static void show(Component03 component03){
        for (Component03 c : component03.getList()) {
            if(c instanceof Leaf03){
                c.todo();
            }else {
                show(c);
            }
        }
    }

}

abstract class Component03{

    private String name;

    public Component03(String name){
        this.name = name;
    }

    public void todo(){
        System.out.println(this.name + "做些什么");
    }

    protected abstract void add(Component03 component03);

    protected abstract List<Component03> getList();

}

class Leaf03 extends Component03{

    public Leaf03(String name) {
        super(name);
    }

    @Deprecated
    protected void add(Component03 component03) {

    }

    @Deprecated
    protected List<Component03> getList() {
        return null;
    }
}


class Composite03 extends Component03{

    private List<Component03> childList = new ArrayList<>();

    public Composite03(String name){
        super(name);
    }

    @Override
    protected void add(Component03 component03) {
        this.childList.add(component03);
    }

    @Override
    protected List<Component03> getList() {
        return this.childList;
    }
}

